package seqList.LeetCode;
//LeetCode 第142题  环形链表返回回环开始位置
//利用数学可以推算出从开始到入口的长度 == 相遇点到入口的长度。
public class Num142 {
    public ListNode detectCycle(ListNode head) {
        //快慢指针
        ListNode fast = head;
        ListNode low = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            low = low.next;
            //有回环
            if (fast == low) {
                ListNode third = head;
                while (third != low) {
                    third = third.next;
                    low = low.next;
                }
                return third;
            }
        }
        return null;
    }
}
